package ve.basura.entrada.bdl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import ve.basura.entrada.util.EntradaSalidaUtil;


import com.aseo.business.hibernate.ChoferManager;
import com.aseo.business.hibernate.MomentoManager;
import com.aseo.business.hibernate.ParadaVehiculoManager;
import com.aseo.business.hibernate.RecorridoVehiculoManager;
import com.aseo.business.hibernate.SitioManager;
import com.aseo.business.hibernate.UsuarioManager;
import com.aseo.business.hibernate.VehiculoManager;

import com.aseo.model.entity.hibernate.Chofer;
import com.aseo.model.entity.hibernate.Momento;
import com.aseo.model.entity.hibernate.ParadaVehiculo;
import com.aseo.model.entity.hibernate.RecorridoVehiculo;
import com.aseo.model.entity.hibernate.Sitio;
import com.aseo.model.entity.hibernate.Usuario;
import com.aseo.model.entity.hibernate.Vehiculo;

public class EntradaSalidaBDL implements EntradaSalidaBDLInterface {
	
	/**
	 * Managers a utilizar.
	 */
	@Autowired
	VehiculoManager vehiculoManager;
	@Autowired
	ChoferManager choferManager;
	@Autowired 
	ParadaVehiculoManager paradaVehiculoManager;
	@Autowired
	SitioManager sitioManager;
	@Autowired
	UsuarioManager usuarioManager;
	@Autowired
	MomentoManager momentoManager;
	@Autowired
	RecorridoVehiculoManager recorridoVehiculoManager;
	
	EntradaSalidaUtil entradaSalidaUtil;
	
	public EntradaSalidaBDL(EntradaSalidaUtil entradaSalidaUtil ) {
		this.entradaSalidaUtil =  entradaSalidaUtil;
	}
	
	public EntradaSalidaBDL(){
		
	}	
	
	/* (non-Javadoc)
	 * @see ve.basura.entrada.bdl.EntradaSalidaBDLInterface#getChoferByCI(java.lang.String)
	 */
	@Override
	public Chofer getChoferByCI(String ci) {
		List<Chofer> list = choferManager.getChoferByCI(ci);
		return (list.size()>0) ? list.get(0) : null ;
	}
	
	/* (non-Javadoc)
	 * @see ve.basura.entrada.bdl.EntradaSalidaBDLInterface#getChoferByCodigo(java.lang.String)
	 */
	@Override
	public Chofer getChoferByCodigo(String codigo) {
		List<Chofer> list = choferManager.getChoferByCodigo(codigo);
		return (list.size()>0) ? list.get(0) : null ;
	}

	/* (non-Javadoc)
	 * @see ve.basura.entrada.bdl.EntradaSalidaBDLInterface#getVehiculoByPlaca()
	 */
	@Override
	public Vehiculo getVehiculoByPlaca(String placa) {
		List<Vehiculo> list = vehiculoManager.getVehiculoByPlaca(placa);
		return (list.size()>0) ? list.get(0) : null ;
	}

	/* (non-Javadoc)
	 * @see ve.basura.entrada.bdl.EntradaSalidaBDLInterface#getVehiculoByCodigo(java.lang.String)
	 */
	@Override
	public Vehiculo getVehiculoByCodigo(String codigo) {
		List<Vehiculo> list = vehiculoManager.getVehiculoByCodigo(codigo);
		return (list.size()>0) ? list.get(0) : null ;
	}

	@Override
	public boolean persistParada(Vehiculo vehiculo, Chofer chofer, String peso,
			boolean entrada) {

		
		Sitio sitio = sitioManager.get(new Long(entradaSalidaUtil.getConfigProperty("basura.config.sitio")).longValue());
		Usuario usuario =(Usuario) usuarioManager.get(new Long(entradaSalidaUtil.getConfigProperty("basura.config.usuario")).longValue());
		
		//Obtenemos el recorrido 
		RecorridoVehiculo recorridoVehiculo = null;
		
		//Recorrido vehiculo
		
		Calendar calendar = Calendar.getInstance();
		String fecha = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)).concat(String.valueOf(calendar.get(Calendar.MONTH)))+String.valueOf(calendar.get(Calendar.YEAR));
		int fechaInt = Integer.parseInt(fecha);
		
		if(entrada){ //entrada
			List<RecorridoVehiculo> listaRecorrido =  recorridoVehiculoManager.getByVehiculoChofer(vehiculo, chofer, fechaInt);
			
			if(listaRecorrido.size()>0){
				recorridoVehiculo =  listaRecorrido.get(0);
			}else{
				return false;
		}	
			
		}else{ //salida
			recorridoVehiculo = new RecorridoVehiculo();
			recorridoVehiculo.setChoferId(chofer);
			recorridoVehiculo.setVehiculoId(vehiculo);
			recorridoVehiculo.setFechaRegistro(new Date());
			recorridoVehiculo.setFechaActualizacion(new Date());
			recorridoVehiculo.setFechaRecorrido(fechaInt);
			recorridoVehiculo.setEstado(1);
			recorridoVehiculoManager.persist(recorridoVehiculo);
		}
		
		//Momento
		Momento momento = null;
		
		if(entrada)
			momento = momentoManager.getMomentoEntrada();
		else
			momento = momentoManager.getMomentoSalida();
		
		ParadaVehiculo paradaVehiculo = new ParadaVehiculo();
		
		paradaVehiculo.setSitioId(sitio);
		paradaVehiculo.setUsuarioId(usuario);
		paradaVehiculo.setEstado(1);
		paradaVehiculo.setMomentoId(momento);
		paradaVehiculo.setPeso(new BigDecimal(peso));
		paradaVehiculo.setFechaActualizacion(new Date());
		paradaVehiculo.setFechaRegistro(new Date());
		paradaVehiculo.setRecorridoVehiculoId(recorridoVehiculo);
		
		
		if(entrada)
			paradaVehiculo.setHoraEntrada(new Date());
		else
			paradaVehiculo.setHoraSalida(new Date());
		
		paradaVehiculoManager.persist(paradaVehiculo);
		
		return true;
	}
	
	/**
	 * Encargado de retornar una lista de vehiculos
	 * @return
	 */
	public  List<Vehiculo> getVehiculos(){
		return vehiculoManager.getAll();
	}

	/**
	 * Encargado de retornar una lista de choferes
	 * @return
	 */
	public  List<Chofer> getChoferes(){
		return choferManager.getAll();
	}
	
	/**
	 * @param vehiculoManager the vehiculoManager to set
	 */
	public void setVehiculoManager(VehiculoManager vehiculoManager) {
		this.vehiculoManager = vehiculoManager;
	}

	/**
	 * @param chhoferManager the chhoferManager to set
	 */
	public void setChoferManager(ChoferManager choferManager) {
		this.choferManager = choferManager;
	}
	
	/**
	 * @param paradaVehiculoManager the paradaVehiculoManager to set
	 */
	public void setParadaVehiculoManager(ParadaVehiculoManager paradaVehiculoManager) {
		this.paradaVehiculoManager = paradaVehiculoManager;
	}

	/**
	 * @param sitioManager the sitioManager to set
	 */
	public void setSitioManager(SitioManager sitioManager) {
		this.sitioManager = sitioManager;
	}

	/**
	 * @param usuarioManager the usuarioManager to set
	 */
	public void setUsuarioManager(UsuarioManager usuarioManager) {
		this.usuarioManager = usuarioManager;
	}

	/**
	 * @return the momentManager
	 */
	public MomentoManager getMomentoManager() {
		return momentoManager;
	}

	/**
	 * @param momentManager the momentManager to set
	 */
	public void setMomentoManager(MomentoManager momentoManager) {
		this.momentoManager = momentoManager;
	}

	/**
	 * @param recorridoVehiculoManager the recorridoVehiculoManager to set
	 */
	public void setRecorridoVehiculoManager(
			RecorridoVehiculoManager recorridoVehiculoManager) {
		this.recorridoVehiculoManager = recorridoVehiculoManager;
	}

	
	
}
